﻿using System.Activities;
using System.Activities.Expressions;
using System.Activities.Statements;
using MvcFlow.Mvc;
using MvcFlow.Mvc.ActionResults;

namespace MvcFlow.Activities
{
    public class ViewInteractionPointActivity : Activity<HttpRequestData>
    {
        public InArgument<string>   ViewName        { get; set; }
        public InArgument<string>   LayoutName      { get; set; } 
        public InArgument<bool>     IsPartialView   { get; set; }
        public InArgument<object>   Model           { get; set; }

        private Activity CreateImplementation()
        {
            var viewNameVariable        = new Variable<string>(c => c.GetValue(ViewName));
            var layoutNameVariable      = new Variable<string>(c => c.GetValue(LayoutName));
            var isPartialViewVariable   = new Variable<bool>(c => c.GetValue(IsPartialView));
            var modelVariable           = new Variable<object>(c => c.GetValue(Model));

            var actionResultVariable    = new Variable<IActionResult>("actionResult");
       
            var result = new Sequence()
            {
                Variables =
                {
                    viewNameVariable,
                    layoutNameVariable,
                    isPartialViewVariable,
                    modelVariable,
                    actionResultVariable,
                },
                Activities =
                {
                    new ViewActionResultBuilderActivity()
                    {
                        ViewName        = viewNameVariable,
                        LayoutName      = layoutNameVariable,
                        IsPartialView   = isPartialViewVariable,
                        Model           = modelVariable,
                        Result          = actionResultVariable
                    },

                    new HttpInteractionPointActivity()
                    {
                        ActionResult    = actionResultVariable,
                        Result          = Result,
                    }
                },
            };
         
            return result;
        }

        public ViewInteractionPointActivity()
        {
            this.Implementation = CreateImplementation;
            DisplayName = "View";
        }
    }
}